package forgotyourpassword;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;

import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Email_link extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  
    @Test
  public void testUntitled8() throws Exception {
    	
    	class MailAuthenticator extends Authenticator {
    		 
    	    public PasswordAuthentication getPasswordAuthentication() {
    	         return new PasswordAuthentication("quality_assurance@bk.ru", "parol123");
    	    }
    	}
    	
    	Properties props = new Properties();
        
	       String host = "pop.mail.ru";
	       String provider = "pop3";
	 
	        Session session = Session.getDefaultInstance(props,
	                new MailAuthenticator());
	        
	        
	        Store store = session.getStore(provider);
	        store.connect(host, null, null);
	 
	        Folder inbox = store.getFolder("INBOX");
	        inbox.open(Folder.READ_ONLY);
	        
	        Message[] messages = inbox.getMessages();
	        
	           
	        GetMulti gmulti = new GetMulti();
	        String textMessage = gmulti.getText(messages[messages.length - 1]);
	        String regex = "https?://(.*)/customer/account/resetpassword/(.*)\\S ";
	        Pattern p = Pattern.compile(regex);
	        Matcher m = p.matcher(textMessage);
	        if (m.find()) {
	        	String testtest = "http://dev-mage.com/default/181/index.php/default/customer/account/resetpassword/";
	        	 driver.get(testtest + m.group(2));
	        	 try {
	        	        assertTrue(isElementPresent(By.id("password")));
	        	      } catch (Error e) {
	        	        verificationErrors.append(e.toString());
	        	      }
	        	      try {
	        	        assertTrue(isElementPresent(By.id("confirmation")));
	        	      } catch (Error e) {
	        	        verificationErrors.append(e.toString());
	        	      }
	        	      try {
	        	        assertTrue(isElementPresent(By.cssSelector("div.buttons-set > button.button")));
	        	      } catch (Error e) {
	        	        verificationErrors.append(e.toString());
	        	      }
	        	   
	        }
	        inbox.close(false);
	        store.close();   	  
   
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
  

	public class GetMulti {
	    
	    public  boolean textIsHtml = false;
	 
	    /**
	     * Return the primary text content of the message.
	     */
	    public String getText(Part p) throws MessagingException, IOException {
	        if (p.isMimeType("text/*")) {
	            String s = (String)p.getContent();
	            textIsHtml = p.isMimeType("text/html");
	            return s;
	        }
	 
	        if (p.isMimeType("multipart/alternative")) {
	            // prefer html text over plain text
	            Multipart mp = (Multipart)p.getContent();
	            String text = null;
	            for (int i = 0; i < mp.getCount(); i++) {
	                Part bp = mp.getBodyPart(i);
	                if (bp.isMimeType("text/plain")) {
	                    if (text == null)
	                        text = getText(bp);
	                    continue;
	                } else if (bp.isMimeType("text/html")) {
	                    String s = getText(bp);
	                    if (s != null)
	                        return s;
	                } else {
	                    return getText(bp);
	                }
	            }
	            return text;
	        } else if (p.isMimeType("multipart/*")) {
	            Multipart mp = (Multipart)p.getContent();
	            for (int i = 0; i < mp.getCount(); i++) {
	                String s = getText(mp.getBodyPart(i));
	                if (s != null)
	                    return s;
	            }
	        }
	 
	        return null;
	    }
	 
	}

}
